package site.wlwsjsx.iot_backend.mapper;

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import site.wlwsjsx.iot_backend.dto.WorkstationDetailDTO;
import site.wlwsjsx.iot_backend.entity.UserInfo;
import site.wlwsjsx.iot_backend.entity.WorkstationEnv;
import site.wlwsjsx.iot_backend.entity.WorkstationInfo;

import java.util.List;

@SpringBootTest
public class WorkstationInfoTest {
    @Resource
    private LabEnvMapper labEnvMapper;
    @Resource
    private LabInfoMapper labInfoMapper;

    @Resource
    private WorkstationInfoMapper workstationInfoMapper;

    @Resource
    private WorkstationEnvMapper workstationEnvMapper;

    @Resource
    private UserMapper userMapper;

    @Test
    public void updateById() {

    }

    @Test
    public void test() {
        List<WorkstationInfo> workstationInfos = workstationInfoMapper.selectByLabId(1);
        for (WorkstationInfo workstationInfo : workstationInfos) {
            WorkstationDetailDTO workstationDetailDTO = new WorkstationDetailDTO();
            if (workstationInfo != null) {
                workstationDetailDTO.setId(workstationInfo.getId());
                workstationDetailDTO.setWorkstationCode(workstationInfo.getWorkstationCode());
                workstationDetailDTO.setWorkstationStatus(workstationInfo.getWorkstationStatus());
                workstationDetailDTO.setCurUserId(workstationInfo.getCurUserId());
                // 控制器状态
                workstationDetailDTO.setPowerStatus(workstationInfo.getPowerStatus());
                workstationDetailDTO.setLightStatus(workstationInfo.getLightStatus());
                workstationDetailDTO.setRelayNum1Status(workstationInfo.getRelayNum1Status());
                workstationDetailDTO.setRelayNum2Status(workstationInfo.getRelayNum2Status());
            }
            if (workstationInfo.getCurUserId() != null) {
                // 调用 mapper 查询使用者数据
                UserInfo userInfo = userMapper.selectUserById(workstationInfo.getCurUserId());
                if (userInfo != null) {
                    workstationDetailDTO.setUsername(userInfo.getUsername());
                    workstationDetailDTO.setRealName(userInfo.getRealName());
                    workstationDetailDTO.setModifyTime(workstationInfo.getModifyTime());
                }
            }

            // 调用 mapper 查询工位环境数据
            WorkstationEnv workstationEnv = workstationEnvMapper.selectLatestByCode(workstationInfo.getWorkstationCode());
            if (workstationEnv != null) {
                workstationDetailDTO.setTemperature(workstationEnv.getTemperature());
                workstationDetailDTO.setHumidity(workstationEnv.getHumidity());
                workstationDetailDTO.setSmokeScope(workstationEnv.getSmokeScope());
                workstationDetailDTO.setLightIntensity(workstationEnv.getLightIntensity());
                workstationDetailDTO.setFlameScope(workstationEnv.getFlameScope());
            }
            System.out.println(workstationDetailDTO);
        }
    }
}
